package net.esj.background.support.multi;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import net.esj.basic.exception.MelonException;
import net.esj.basic.utils.GetClassLoader;
import net.esj.basic.utils.SessionKeyConstants;

public final class Portal {
	
	private String theme;
	private String template;
	private String namespace;
	private String loginManage;
	private String menuManage;
	private String sessionKey;
	private String userInfoHref;
	private long timeout;
	
	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public String getLoginManage() {
		return loginManage;
	}

	public void setLoginManage(String loginManage) {
		this.loginManage = loginManage;
	}

	public String getMenuManage() {
		return menuManage;
	}

	public void setMenuManage(String menuManage) {
		this.menuManage = menuManage;
	}

	public Class getMenuManageClass() throws ClassNotFoundException{
		return Class.forName(menuManage);
	}
	
	public Class getLoginManageClass() throws ClassNotFoundException{
		return Class.forName(loginManage);
	}

	public String getSessionKey() {
		return sessionKey;
	}

	public void setSessionKey(String sessionKey) {
		this.sessionKey = sessionKey;
	}

	public String getUserInfoHref() {
		return userInfoHref;
	}

	public void setUserInfoHref(String userInfoHref) {
		this.userInfoHref = userInfoHref;
	}

	public long getTimeout() {
		return timeout;
	}

	public void setTimeout(long timeout) {
		this.timeout = timeout;
	}



	private static final String DEFAULT_PORTAL_CONFIG_DIR = "portal";
	
	public static final String PORTAL_KEY_THEME ="theme";
	
	public static final String DEFAULT_THEME = "default";
	
	public static final String PORTAL_KEY_TEMPLATE ="template";
	
	public static final String DEFAULT_TEMPLATE = "easyui";
	
	public static final String PORTAL_KEY_NAMESPACE="namespace";
	
	public static final String DEFAULT_NAMESPACE = "/backstage-manager";
	
	public static final String PORTAL_KEY_LOGINMANAGE="manage.login";
	
	public static final String DEFAULT_LOGINMANAGE = "net.esj.auth.inst.hibernate.dependent.background.manage.AuthBgLogin";
	
	public static final String PORTAL_KEY_MENUMANAGE="manage.menu";
	
	public static final String DEFAULT_MENUMANAGE = "net.esj.auth.inst.hibernate.dependent.background.manage.AuthBgMenu";
	
	public static final String PORTAL_KEY_SESSIONKEY = "session.key";
	
	public static final String DEFAULT_SESSIONKEY = SessionKeyConstants.LOGIN_USER;
	
	public static final String PORTAL_SESSION_TIMEOUT = "session.timeout";
	
	public static final long DEFAULT_SESSION_TIMEOUT = 30;
	
	public static final String PORTAL_KEY_USER_INFO_HREF = "user.info.url";
	
	
	private static Map<String, Portal> portalMap = new HashMap<String, Portal>();
	
	public static final  Portal DEFAULT_PORTAL = new Portal();
	static{
		DEFAULT_PORTAL.setLoginManage(DEFAULT_LOGINMANAGE);
		DEFAULT_PORTAL.setMenuManage(DEFAULT_MENUMANAGE);
		DEFAULT_PORTAL.setNamespace(DEFAULT_NAMESPACE);
		DEFAULT_PORTAL.setTemplate(DEFAULT_TEMPLATE);
		DEFAULT_PORTAL.setTheme(DEFAULT_THEME);
		DEFAULT_PORTAL.setSessionKey(DEFAULT_SESSIONKEY);
		DEFAULT_PORTAL.setTimeout(DEFAULT_SESSION_TIMEOUT);
	}
	
	public static final Portal getPortal(String key){
		if(portalMap.isEmpty()){
			reload();
		}
		return portalMap.get(key);
	}
	
	public static Collection<Portal> getAll(){
		if(portalMap.isEmpty()){
			reload();
		}
		return portalMap.values();
	}
	
	private static void reload(){
		URL url = GetClassLoader.fromContext().getResource("");
		File dir = new File(url.getPath()+File.separator+DEFAULT_PORTAL_CONFIG_DIR);
		if(!dir.exists()){
			return;
		}
		File[] listFiles = dir.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String name) {
				if(name.endsWith(".properties")){
					return true;
				}
				return false;
			}
		});
		for(File f: listFiles){
			Portal p =load(f);
			portalMap.put(f.getName(), p);
		}
		 portalMap.put("default", DEFAULT_PORTAL);
	}
	
	private static Portal load(File f){
		if(!f.exists() || f.isDirectory()){
			return null;
		}
		Properties properties = new Properties();
		Portal portal = new Portal();
		
		InputStream in = null;
		try {
			in =f.toURL().openStream();
			properties.load(in);
			
			portal.setLoginManage(properties.getProperty(PORTAL_KEY_LOGINMANAGE, DEFAULT_LOGINMANAGE));
			portal.setMenuManage(properties.getProperty(PORTAL_KEY_MENUMANAGE, DEFAULT_MENUMANAGE));
			portal.setNamespace(properties.getProperty(PORTAL_KEY_NAMESPACE,DEFAULT_NAMESPACE));
			portal.setTemplate(properties.getProperty(PORTAL_KEY_TEMPLATE, DEFAULT_TEMPLATE));
			portal.setTheme(properties.getProperty(PORTAL_KEY_THEME, DEFAULT_THEME));
			portal.setSessionKey(properties.getProperty(PORTAL_KEY_SESSIONKEY, DEFAULT_SESSIONKEY));
			portal.setUserInfoHref(properties.getProperty(PORTAL_KEY_USER_INFO_HREF));
			portal.setTimeout(Long.valueOf(properties.getProperty(PORTAL_SESSION_TIMEOUT, "0")));
		} catch (IOException e) {
			throw new MelonException("Could not load " + f.getPath() + ".properties:"
					+ e, e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException io) {
					
				}
			}
		}
		return portal;
	}
	
	public static final void main(String[] args){
		reload();
	}
}
